package com.lg.partition42;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/10 9:45
 */
public class P4180严格次小生成树 {

    private static final int N = 100010, M = 300010, D = (int) (Math.log(N) / Math.log(2)), INF = 0x3f3f3f3f;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    private static Edge[] edges = new Edge[M];
    private static int[] parent = new int[N], q = new int[N], depth = new int[N], dist = new int[N];
    //dist1[i][j]、dist2[i][j]分别表示节点i向上跳2^j步之内的最大边权和次大边权
    private static int[][] f = new int[N][D + 1], dist1 = new int[N][D + 1], dist2 = new int[N][D + 1];

    private static int n, m, idx = 0, es = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        Arrays.fill(h, -1);
        for (int i = 0; i < m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            if (a == b) continue;
            edges[es++] = new Edge(a, b, c, false);
        }
        long mst = kruskal(), ans = (long) 1e18;
        bfs(1);
        for (int i = 0; i < es; i++) {
            if (!edges[i].flag) {
                int u = edges[i].u, v = edges[i].v, w = edges[i].w;
                ans = Math.min(ans, mst + lca(u, v, w));
            }
        }
        writer.write(ans + "\n");
        writer.flush();
    }

    private static int lca(int x, int y, int w) {
        int idx = 0;
        if (depth[x] < depth[y]) {
            int t = x;
            x = y;
            y = t;
        }
        for (int i = D; i >= 0; i--) {
            if (depth[f[x][i]] >= depth[y]) {
                dist[idx++] = dist1[x][i];
                dist[idx++] = dist2[x][i];
                x = f[x][i];
            }
        }
        if (x != y) {
            for (int i = D; i >= 0; i--) {
                if (f[x][i] != f[y][i]) {
                    dist[idx++] = dist1[x][i];
                    dist[idx++] = dist2[x][i];
                    dist[idx++] = dist1[y][i];
                    dist[idx++] = dist2[y][i];
                    x = f[x][i];
                    y = f[y][i];
                }
            }
            dist[idx++] = dist1[x][0];
            dist[idx++] = dist1[y][0];
        }
        int d1 = -INF, d2 = -INF;
        for (int i = 0; i < idx; i++) {
            if (dist[i] > d1) {
                d2 = d1;
                d1 = dist[i];
            } else if (d1 != dist[i] && dist[i] > d2) {
                d2 = dist[i];
            }
        }
        if (w > d1) return w - d1;
        if (w > d2) return w - d2;
        return INF;
    }

    private static void bfs(int root) {
        int hh = 0, tt = 0;
        q[0] = root;
        depth[root] = 1;
        while (hh <= tt) {
            int u = q[hh++];
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (depth[v] != 0) continue;
                depth[v] = depth[u] + 1;
                f[v][0] = u;
                dist1[v][0] = w[i];
                dist2[v][0] = -INF;
                for (int j = 1; j <= D; j++) {
                    int anc = f[v][j - 1];
                    f[v][j] = f[anc][j - 1];
                    int[] temp = new int[]{ dist1[v][j - 1], dist2[v][j - 1], dist1[anc][j - 1], dist2[anc][j - 1] };
                    dist1[v][j] = dist2[v][j] = -INF;
                    for (int d : temp) {
                        if (d > dist1[v][j]) {
                            dist2[v][j] = dist1[v][j];
                            dist1[v][j] = d;
                        } else if (d != dist1[v][j] && d > dist2[v][j]) {
                            dist2[v][j] = d;
                        }
                    }
                }
                q[++tt] = v;
            }
        }
    }

    private static long kruskal() {
        Arrays.sort(edges, 0, es);
        for (int i = 0; i <= n; i++) parent[i] = i;
        long res = 0;
        for (int i = 0; i < es; i++) {
            int u = edges[i].u, v = edges[i].v, w = edges[i].w;
            int a = find(u), b = find(v);
            if (a != b) {
                res += w;
                parent[a] = b;
                add(u, v, w);
                add(v, u, w);
                edges[i].flag = true;
            }
        }
        return res;
    }

    private static int find(int x) {
        if (parent[x] != x) parent[x] = find(parent[x]);
        return parent[x];
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static class Edge implements Comparable<Edge> {
        private int u, v, w;
        private boolean flag;

        public Edge(int u, int v, int w, boolean flag) {
            this.u = u;
            this.v = v;
            this.w = w;
            this.flag = flag;
        }

        @Override
        public int compareTo(Edge o) {
            return w - o.w;
        }
    }
}
